package com.bridgeintelligent.tag.webserver.dim.mgmt.service.index;

import com.bridgeintelligent.tag.mapper.CatalogMapper;
import com.bridgeintelligent.tag.mapper.IndexMapper;
import com.bridgeintelligent.tag.mapper.IndexOtherMapper;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimCatalog;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.model.DimCountModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @author: grp 2021-01-27 14:47
 **/
@Service
@AllArgsConstructor
@Slf4j
public class IndexOtherServiceImpl implements IndexOtherService{

    private final IndexOtherMapper indexOtherMapper;

    private final CatalogMapper catalogMapper;

    private final IndexMapper indexMapper;

    @Override
    public List<AssessPieVO> assessPie(String catalogId) {
        if(StringUtils.equals(catalogId,"all")){
            return indexOtherMapper.countByCatalogIds(new ArrayList<>());
        }
        List<DimCatalog> all = getDimCatalogs(catalogId);
        return indexOtherMapper.countByCatalogIds(all);
    }

    @Override
    public List<AssessDataVO> assessData(String group, String catalogId) {
        Integer groupValue = NumberUtils.isDigits(group) ?Integer.parseInt(group):0;
        if(StringUtils.equals(catalogId,"all")){
            return indexOtherMapper.countDims(groupValue, new ArrayList<>());
        }
        List<DimCatalog> all = getDimCatalogs(catalogId);
        return indexOtherMapper.countDims(groupValue, all);
    }

    @Override
    public CountVO dimCount(DimCountModel model) {
        List<DimCountDateVO> histogram = getDimCountDateVOS(model);
        List<Map<String, DimCountVO>> table = getTableData(histogram);
        return new CountVO().setHistogram(histogram).setTable(table);
    }

    private List<DimCountDateVO> getDimCountDateVOS(DimCountModel model) {
        List<DimCountDateVO> histogram = new ArrayList<>();
        List<IndexDimCatalogVO> dimCatalogVOList = indexMapper.dimCatalog();
        Map<String, List<DimCatalog>> parentCatalogMapping = new HashMap<>();
        Map<String, String> nameMapping = new HashMap<>();
        dimCatalogVOList.forEach(catalog->{
            nameMapping.put(catalog.getId(),catalog.getName());
            parentCatalogMapping.put(catalog.getId(),getDimCatalogs(catalog.getId()));
        });
        model.getDates().forEach(date->histogram.add(getHistogramData(date, parentCatalogMapping, nameMapping)));
        return histogram;
    }

    private List<Map<String, DimCountVO>> getTableData(List<DimCountDateVO> histogram) {
        Map<String, Map<String, DimCountVO>> mapping = new HashMap<>();
        histogram.forEach(element->{
            element.getInfos().forEach(info->{
                String catalogId = info.getCatalogId();
                Map<String, DimCountVO> dateMapping = mapping.computeIfAbsent(catalogId, k -> new HashMap<>());
                dateMapping.put(element.getDate(), info);
            });
        });
        List<Map<String, DimCountVO>> result = new ArrayList<>();
        mapping.forEach((k,v)->result.add(v));
        return result;
    }

    private DimCountDateVO getHistogramData(String date, Map<String, List<DimCatalog>> parentCatalogMapping, Map<String, String> nameMapping) {
        List<DimCountVO> infos = new ArrayList<>();
        parentCatalogMapping.forEach((k,v)->{
            Integer count = indexOtherMapper.countDimsByDate(date, v);
            infos.add(new DimCountVO().setCount(count).setCatalogId(k).setName(nameMapping.get(k)));
        });
        return new DimCountDateVO().setDate(date).setInfos(infos);
    }

    @Override
    public List<DimActiveVO> dimActive() {
        List<DimActiveVO> result = new ArrayList<>();
        List<IndexDimCatalogVO> dimCatalogVOList = indexMapper.dimCatalog();
        for (IndexDimCatalogVO indexDimCatalogVO : dimCatalogVOList) {
            Integer count = indexOtherMapper.countDimsByCatalogId(getDimCatalogs(indexDimCatalogVO.getId()));
            Integer active = indexOtherMapper.countActivesByCatalogId(getDimCatalogs(indexDimCatalogVO.getId()),
                    DateHelper.currentDateTime("yyyy-MM-dd"));
            result.add(new DimActiveVO().setName(indexDimCatalogVO.getName()).setCatalogId(indexDimCatalogVO.getId())
                    .setCount(count).setActiveCount(active));
        }
        return result;
    }

    private List<DimCatalog> getDimCatalogs(String catalogId) {
        List<DimCatalog> dimCatalogs = catalogMapper.findNext(catalogId);
        List<DimCatalog> all = new ArrayList<>(dimCatalogs);
        all.add(new DimCatalog().setId(catalogId));
        for (DimCatalog catalog : dimCatalogs) {
            all.addAll(catalogMapper.findNext(catalog.getId()));
        }
        return all;
    }
}
